<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <BreakSpace label="操作记录" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable ref="table" :isSelectColumn="false" height="auto" :columns="columns" :dataSource="tableLists" :onColumnsChange="columns => (this.columns = columns)"> </FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <multiuse-button type="primary" v-show="type != 'show'" :click="commitHandle">通 过</multiuse-button>
      <multiuse-button type="primary" v-show="type != 'show'" :click="rejectHandle">驳 回</multiuse-button>
      <el-button @click="closeDrawer">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getDetailById, managerSubmit, managerReject } from '@/api/omFinance/FinanceManage/FinancialManagerApproval';
import { getDealerUser, queryLog } from '@/api/omFinance';

export default {
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'show'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {},
      fetchApi: () => {},
      formData: {},
      tableLists: []
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      if (this.type != 'add') {
        this.getUser();
        this.getRecordLog();
        this.getDetailById();
      }

      if (this.type == 'show') {
        this.formList.map(x => {
          if (x.rules) {
            x.rules[0].required = false;
          }
        });
      }
    },
    async getDetailById() {
      const res = await getDetailById(this.rowData.id);
      if (res.resultCode == 200) {
        this.formList.forEach(x => {
          x.initialValue = res.data[x.fieldName];
        });
      }
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async getUser() {
      let res = await getDealerUser();
      let empList = res.data.map(x => ({ text: x.userName, value: x.userId }));
      this.columns.find(item => item.dataIndex === 'updated_by').editItems = empList; //操作姓名
      this.columns.find(item => item.dataIndex === 'updated_by').filterItems = empList;
    },
    async getRecordLog() {
      const res = await queryLog(this.rowData.id);
      if (res.resultCode == 200) {
        this.tableLists = res.data || [];
      }
    },
    async commitHandle() {
      this.formList.find(x => x.fieldName == 'financialCheckRemark').rules[0].required = false;

      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let saveData = Object.assign({}, this.rowData, this.saveData);
        console.log(saveData, '---提报的数据---');
        const res = await managerSubmit(saveData);
        if (res.resultCode === 200) {
          await this.closeDrawer('refresh');
        }
      }
    },
    async rejectHandle() {
      this.formList.find(x => x.fieldName == 'financialCheckRemark').rules[0].required = true;
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let saveData = Object.assign({}, this.rowData, this.saveData);
        console.log(saveData, '---提报的数据---');
        const res = await managerReject(saveData);
        if (res.resultCode === 200) {
          await this.closeDrawer('refresh');
        }
      }
    },
    closeDrawer(type) {
      this.$emit('close', false, type);
    },
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '三方融资'
        },
        {
          type: 'INPUT',
          label: '申请单号',
          fieldName: 'applyNo',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '银行名称',
          fieldName: 'bankName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '出票金额（万）',
          fieldName: 'billAmount',
          placeholder: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '出票期限',
          fieldName: 'billDeadline', // TODO 字典6027
          placeholder: '',
          itemList: this.createDictList('6027'),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '银行联系人',
          fieldName: 'linkMan',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '银行联系电话',
          fieldName: 'linkPhone',
          placeholder: '',
          disabled: true
        },
        {
          type: 'DATE',
          label: '申请日期',
          fieldName: 'applyDate',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '提车公司',
          fieldName: 'salesCompany', // TODO 字典2555
          placeholder: '',
          itemList: this.createDictList('2555'),
          disabled: true
        },
        {
          type: 'SELECT',
          label: '收款方',
          offsetRightCols: 1,
          fieldName: 'payee',
          placeholder: '',
          itemList: this.createDictList('6031'),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '预提车型',
          selfCols: 2,
          fieldName: 'reservedModel',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'applyRemark',
          placeholder: '',
          selfCols: 3,
          disabled: true
        },
        {
          type: 'BREAK_SPACE',
          label: '审批参数'
        },
        {
          type: 'INPUT',
          label: '本月已开票金额',
          fieldName: 'invoiceAmount',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '上月终端',
          fieldName: 'lastSalesQuantity',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '前三月平均终端',
          fieldName: 'averageSalesQuantity',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '当前库存数',
          fieldName: 'stockQuantity',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '库存系数',
          fieldName: 'stockFactor',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '库存系数上限',
          fieldName: 'stockFactorLimit',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '目前库销比',
          fieldName: 'stockSalesRatio',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '超期车台数',
          fieldName: 'overdueQuantity',
          placeholder: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '是否风险关注店',
          fieldName: 'isRiskDealer',
          placeholder: '',
          itemList: this.createDictList('1004'),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '移动比例',
          fieldName: 'moveRatio',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '回款率',
          fieldName: 'collectionRate',
          placeholder: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '经销商经营状态',
          fieldName: 'dealerStatus',
          placeholder: '',
          disabled: true,
          itemList: this.createDictList('2552')
        },
        {
          type: 'TEXT_AREA',
          label: '审批意见',
          fieldName: 'financialCheckRemark',
          selfCols: 3,
          placeholder: '',
          rules: [{ required: false, message: '请输入审批意见', trigger: 'blur' }]
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作状态',
          dataIndex: 'businessStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6030'),
          render: p => {
            return <span>{this.createDictText(p.row.businessStatus, '6030')}</span>;
          }
        },
        {
          title: '操作人员',
          dataIndex: 'createdBy',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '操作时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '审核备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
